home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sun / ax25ip.shr / ax25ether.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-24  |  4.1 KB  |  173 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <net/if.h>
  6. #include <netinet/if_ether.h>
  7. #include <sys/if_ax.h>
  8.  
  9. u_char ax25broadcastaddr[7] = {
  10.     'Q'<<1, 'S'<<1, 'T'<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1
  11. };
  12. static struct ether_addr etherbroadcastaddr = {
  13.     0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  14. };
  15. static struct ether_addr ethernulladdr;
  16.  
  17. /*
  18.  * convert an ax25 address to an ether_addr
  19.  */
  20. ax_ax2ether(axhp, ethp)
  21. u_char *axhp;
  22. struct ether_addr *ethp;
  23. {
  24.     u_char c;
  25.     int i;
  26.  
  27.     for(i=0; i<6; i++)
  28.         printf("%02x.", axhp[i]);
  29.     printf("%02x '", axhp[6]);
  30.  
  31.     for(i=0; i<(7-1); i++)
  32.         printf("%c", axhp[i]>>1);
  33.     printf("-%c' == ", axhp[6]>>1);
  34.  
  35.     if( !bcmp((caddr_t)axhp, (caddr_t)ax25broadcastaddr,
  36.         sizeof(ax25broadcastaddr)) ) {
  37.         bcopy((caddr_t)ðerbroadcastaddr, (caddr_t)ethp,
  38.             sizeof(struct ether_addr));
  39.         goto done;
  40.     }
  41.  
  42.     ethp->ether_addr_octet[0] = 0x99;
  43.     c =  ((((axhp[0]>>1) - ' ') << 2) & 0xfc);
  44.     c |= ((((axhp[1]>>1) - ' ') >> 4) & 0x03);
  45.     ethp->ether_addr_octet[1] = c;
  46.     c =  ((((axhp[1]>>1) - ' ') << 4) & 0xf0);
  47.     c |= ((((axhp[2]>>1) - ' ') >> 2) & 0x0f);
  48.     ethp->ether_addr_octet[2] = c;
  49.     c =  ((((axhp[2]>>1) - ' ') << 6) & 0xc0);
  50.     c |= ((((axhp[3]>>1) - ' ')     ) & 0x3f);
  51.     ethp->ether_addr_octet[3] = c;
  52.     c =  ((((axhp[4]>>1) - ' ') << 2) & 0xfc);
  53.     c |= ((((axhp[5]>>1) - ' ') >> 4) & 0x03);
  54.     ethp->ether_addr_octet[4] = c;
  55.     c =  ((((axhp[5]>>1) - ' ') << 4) & 0xf0);
  56.     c |= ((((axhp[6]>>1) - '0')     ) & 0x0f);
  57.     ethp->ether_addr_octet[5] = c;
  58.  
  59. done:
  60.     for(i=0; i<sizeof(struct ether_addr)-1; i++)
  61.         printf("%02x:", ethp->ether_addr_octet[i]);
  62.     printf("%02x\n", ethp->ether_addr_octet[5]);
  63.     return 0;
  64. }
  65.  
  66. /*
  67.  * convert an ether_addr to an ax25 address
  68.  */
  69. ax_ether2ax(ethp, axhp)
  70. struct ether_addr *ethp;
  71. u_char *axhp;
  72. {
  73.     u_char c;
  74.     int i;
  75.  
  76.     for(i=0; i<sizeof(struct ether_addr)-1; i++)
  77.         printf("%02x:", ethp->ether_addr_octet[i]);
  78.     printf("%02x == ", ethp->ether_addr_octet[5]);
  79.  
  80.     if( !bcmp((caddr_t)ethp, (caddr_t)ðernulladdr,
  81.         sizeof(struct ether_addr))) {
  82.         printf("nothing\n");
  83.         return;
  84.     }
  85.     if( !bcmp((caddr_t)ethp, (caddr_t)ðerbroadcastaddr,
  86.         sizeof(struct ether_addr))) {
  87.         axhp[0] = 'Q' << 1;
  88.         axhp[1] = 'S' << 1;
  89.         axhp[2] = 'T' << 1;
  90.         axhp[3] = ' ' << 1;
  91.         axhp[4] = ' ' << 1;
  92.         axhp[5] = ' ' << 1;
  93.         axhp[6] = '0' << 1;
  94.         goto done;
  95.     }
  96.  
  97.     c = ethp->ether_addr_octet[0];
  98.     if(c != 0x99) {
  99.         printf("[NO MAPPING]\n");
  100.         for(i=0; i<7; i++)
  101.             axhp[i] = (i+1) << 1;
  102.         return;
  103.     }
  104.     c =  (ethp->ether_addr_octet[1] >> 2) & 0x3f;
  105.     axhp[0] = (u_char)((c + ' ') << 1);
  106.     c =  (ethp->ether_addr_octet[1] << 4) & 0x30;
  107.     c |= (ethp->ether_addr_octet[2] >> 4) & 0x0f;
  108.     axhp[1] = (u_char)((c + ' ') << 1);
  109.     c =  (ethp->ether_addr_octet[2] << 2) & 0x3c;
  110.     c |= (ethp->ether_addr_octet[3] >> 6) & 0x03;
  111.     axhp[2] = (u_char)((c + ' ') << 1);
  112.     c =  (ethp->ether_addr_octet[3]     ) & 0x3f;
  113.     axhp[3] = (u_char)((c + ' ') << 1);
  114.     c =  (ethp->ether_addr_octet[4] >> 2) & 0x3f;
  115.     axhp[4] = (u_char)((c + ' ') << 1);
  116.     c =  (ethp->ether_addr_octet[4] << 4) & 0x30;
  117.     c |= (ethp->ether_addr_octet[5] >> 4) & 0x0f;
  118.     axhp[5] = (u_char)((c + ' ') << 1);
  119.     c =  (ethp->ether_addr_octet[5]     ) & 0x0f;
  120.     axhp[6] = (u_char)((c + '0') << 1);
  121.  
  122. done:
  123.     for(i=0; i<6; i++)
  124.         printf("%02x.", axhp[i]);
  125.     printf("%02x '", axhp[6]);
  126.  
  127.     for(i=0; i<6; i++)
  128.         printf("%c", (axhp[i]>>1) & 0x7f );
  129.     printf("-%c'\n", (axhp[6]>>1) & 0x7f );
  130. }
  131.  
  132. main(argc, argv)
  133. char **argv;
  134. {
  135.     u_char ax25[7];
  136.     struct ether_addr eth;
  137.     u_char *cp, c;
  138.     int i;
  139.  
  140.     if(argc<3) {
  141.         fprintf(stderr, "usage: %s af addr\n", argv[0]);
  142.         fprintf(stderr, "\taf=ax25, ether\n");
  143.         exit(0);
  144.     }
  145.  
  146.     if( !strcmp(argv[1], "ax25")) {
  147.         i = 0;
  148.         bzero((caddr_t)ax25, sizeof(ax25));
  149.         for(cp=(u_char *)argv[2]; *cp && (i<6); cp++) {
  150.             if(*cp=='-') {
  151.                 cp++;
  152.                 break;
  153.             }
  154.             c = islower(*cp) ? toupper(*cp) : *cp;
  155.             ax25[i++] = (u_char)(c << 1);
  156.         }
  157.         while(i<6)
  158.             ax25[i++] = (u_char)(' ' << 1);
  159.         while(*cp=='-')
  160.             cp++;
  161.         if(*cp)
  162.             ax25[6] = (u_char)(*cp << 1);
  163.         ax_ax2ether(ax25, ð);
  164.  
  165.     } else if( !strcmp(argv[1], "ether")) {
  166.         bcopy((caddr_t)ether_aton(argv[2]), (caddr_t)ð,
  167.             sizeof(struct ether_addr));
  168.         ax_ether2ax(ð, ax25);
  169.     } else
  170.         printf("what?\n");
  171.     return 0;
  172. }
  173.